package com.njupt.towerssystem.controller;


import com.njupt.towerssystem.domain.ManageInfos;
import com.njupt.towerssystem.services.PowerService;
import com.njupt.towerssystem.services.RegisteUserService;
import com.njupt.towerssystem.services.ManageService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping("/sys")
@CrossOrigin("*")
public class ManagerInfoCtrl {
    @Resource
    private ManageService manageService;
    @Resource
    private RegisteUserService registeUserService;
    @Resource
    PowerService powerService;
    @RequestMapping("/userreg")
    public String reg(ManageInfos mi){
        boolean ret = registeUserService.regUser(mi);
        if(ret){
            return "{\"userreg\":\"success\"}";
        }else{
            return "{\"userreg\":\"fail\"}";
        }
    }
    @RequestMapping("/login")
    public ManageInfos login(
            @RequestParam(required = false,name = "loginname") String loginname,
            @RequestParam(required = false) String loginpwd,
            @RequestParam(required = false) String token
    ){
        return manageService.login(loginname,loginpwd,token);
    }


    @RequestMapping("/userpower")
    public String setuserpower(
            @RequestParam(required = false) int ruid,
            int userid,int powerid,String isgrant){

        if(isgrant.equalsIgnoreCase("y")){
            powerService.grantUserRule(ruid,powerid,userid);
        }else{
            powerService.revokeUserRule(powerid,userid);
        }return "{\"powerset\":\"success\"}";
    }

    @RequestMapping("/insertPower")
    public String regpower(int ruleid,String rulename){
        powerService.addNewRule(ruleid,rulename);
        return "{\"powerreg\":\"success!!!!\"}";
    }
}
